package com.deer.wms.ware.task.web;


import cn.hutool.core.lang.Validator;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.core.text.Convert;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.ware.task.model.ErpPushLog;
import com.deer.wms.ware.task.model.TimedOutLog;
import com.deer.wms.ware.task.model.erpBean.ErpBase;
import com.deer.wms.ware.task.service.ErpPushLogService;
import com.deer.wms.ware.task.service.PushErpService;
import com.deer.wms.ware.task.service.TimedOutLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/timed_out_log")
public class TimedOutLogController {

    @Autowired
    private TimedOutLogService timedOutLogService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    @Value("${erp.baseUrl}")
    private String BaseUrl;

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public Result page(ErpBase criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<TimedOutLog> list = timedOutLogService.findPage(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/retry")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public Result retry(@RequestBody TimedOutLog timedOutLog) {
        HttpResponse execute = HttpRequest.post(BaseUrl + timedOutLog.getRequestUrl())
                .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
                .header(Header.AUTHORIZATION, "Bearer " + pushErpService.getToken())
                .body(timedOutLog.getRequestParams())//表单内容
                .timeout(60000)//超时，毫秒  60000=60秒
                .execute();
        //状态码
        int status = execute.getStatus();
        timedOutLog.setRetryNumber(timedOutLog.getRetryNumber() + 1);
        /**
         字段验证状态码不等于200 抛出异常
         *
         */
        //请求成功关闭
        if (Validator.equal(200, status)) {
            timedOutLog.setEnable(1);
        }
        timedOutLogService.updateById(timedOutLog);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/onClose")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    public Result onClose(@RequestBody TimedOutLog timedOutLog) {
        timedOutLog.setEnable(1);
        timedOutLogService.updateById(timedOutLog);
        return ResultGenerator.genSuccessResult();
    }

}
